﻿using Dapper.Contrib.Extensions;
using System.Data;
using System.Data.SqlClient;
using static Casbin.NET.Adapter.Dapper.DapperDBContext;
using MySql.Data.MySqlClient;
using Dapper;

namespace Casbin.NET.Adapter.Dapper
{
    public static class DapperDBContextExtension<T> where T : class
    {
        public static bool InitDB(string connectionString, DatabaseType databaseType, PrimaryType primaryType)
        {
            try
            {
                //从特性标签中获取表名
                var tAttribute = (TableAttribute)typeof(T).GetCustomAttributes(typeof(TableAttribute), true)[0];
                if (tAttribute == null)
                {
                    return false;
                }
                string dbName = tAttribute.Name;
                if (dbName == "") return false;
                string sql = "";

                switch (databaseType)
                {
                    case DatabaseType.MYSQL:

                        IDbConnection connMYSQL = new MySqlConnection(connectionString);
                        using (connMYSQL)
                        {
                            //1.判断表是否存在，从连接字符串中取出数据库名
                            string CheckTableExists = $"select * from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='dbo' and TABLE_NAME=@dbName";

                            if (connMYSQL.Query(CheckTableExists, new { dbName }).Count() > 0)
                            {
                                return false;
                            }
                            //2.创建默认表
                            string pkType = primaryType == PrimaryType.GuidType ? "char(36) NOT NULL" : "int NOT NULL AUTO_INCREMENT";

                            sql = @$"CREATE TABLE `{dbName}` (
                                        `id` {pkType},
                                        `ptype` varchar(255) DEFAULT NULL,
                                        `v0` varchar(255) DEFAULT NULL,
                                        `v1` varchar(255) DEFAULT NULL,
                                        `v2` varchar(255) DEFAULT NULL,
                                        `v3` varchar(255) DEFAULT NULL,
                                        `v4` varchar(255) DEFAULT NULL,
                                        `v5` varchar(255) DEFAULT NULL,
                                        PRIMARY KEY (`id`),
                                        KEY `IX_casbin_rule_ptype` (`ptype`),
                                        KEY `IX_casbin_rule_v0` (`v0`),
                                        KEY `IX_casbin_rule_v1` (`v1`),
                                        KEY `IX_casbin_rule_v2` (`v2`),
                                        KEY `IX_casbin_rule_v3` (`v3`),
                                        KEY `IX_casbin_rule_v4` (`v4`),
                                        KEY `IX_casbin_rule_v5` (`v5`)
                                        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;";
                            connMYSQL.Execute(sql);
                        }
                        break;

                    case DatabaseType.MSSQL:

                        IDbConnection connMSSQL = new SqlConnection(connectionString);
                        using (connMSSQL)
                        {
                            //1.判断表是否存在，从连接字符串中取出数据库名
                            string CheckTableExists = "select * from dbo.sysobjects where xtype='U' and Name =@dbName";
                            if ((connMSSQL.Query<int>(CheckTableExists, new { dbName }).Count()) > 0)
                            {
                                return false;
                            }
                            //2.创建默认表casbin_rule
                            string pkType = primaryType == PrimaryType.GuidType ? "uniqueidentifier" : "int";

                            sql = @$"CREATE TABLE [dbo].[{dbName}] (
                            [Id] {pkType} DEFAULT (newid()) NOT NULL,
                            [PType] varchar(255) COLLATE Chinese_PRC_CI_AS  NULL,
                            [V0] varchar(255) COLLATE Chinese_PRC_CI_AS  NULL,
                            [V1] varchar(255) COLLATE Chinese_PRC_CI_AS  NULL,
                            [V2] varchar(255) COLLATE Chinese_PRC_CI_AS  NULL,
                            [V3] varchar(255) COLLATE Chinese_PRC_CI_AS  NULL,
                            [V4] varchar(255) COLLATE Chinese_PRC_CI_AS  NULL,
                            [V5] varchar(255) COLLATE Chinese_PRC_CI_AS  NULL,
                            CONSTRAINT [PK_CASBINMODEL] PRIMARY KEY CLUSTERED ([Id])
                            WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]";
                            connMSSQL.Execute(sql);
                        }
                        break;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}